import { _decorator, Component, Node, Vec2, Vec3, SystemEventType, CCBoolean, EventMouse, EventTouch, find } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraController')
export class CameraController extends Component {

    @property(Node)
    target: Node | null = null;

    @property(CCBoolean)
    canWheel = false

    static instance: CameraController
    radius = 3
    radian = 1.1
    autoRorateSpeed = 0.1
    downPos1 = Vec2.ZERO
    downPos2 = Vec2.ZERO
    currPos1 = Vec2.ZERO
    currPos2 = Vec2.ZERO
    currAngle = 135
    preX1 = 0
    preX2 = 0
    preY1 = 0
    preY2 = 0
    touchID1: number | null = null
    touchID2: number | null = null

    onLoad() {
        CameraController.instance = this
    }

    start() {
        let canvasNode = find("Canvas")
        if (canvasNode) {
            canvasNode.on(SystemEventType.TOUCH_START, this.onTouchDown, this)
            canvasNode.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this)
            canvasNode.on(SystemEventType.TOUCH_END, this.onTouchUp, this)
            canvasNode.on(SystemEventType.TOUCH_CANCEL, this.onTouchUp, this)
            //电脑上用鼠标缩放
            canvasNode.on(SystemEventType.MOUSE_WHEEL, this.onMouseWheel, this)
        } else {
            console.error("场景内找不到Canvas")
        }
    }

    update(deltaTime: number) {
        let targetPos = Vec3.ZERO
        if (this.target) {
            //目标旋转
            this.target.setRotationFromEuler(0, this.currAngle, 0)
            //目标位置
            targetPos = this.target.getPosition()
        }

        //摄影机位置
        let pos = new Vec3(targetPos.x, targetPos.y, targetPos.z)
        pos.y += Math.cos(this.radian) * this.radius
        pos.z += Math.sin(this.radian) * this.radius
        this.node.position = pos
        this.node.lookAt(targetPos)

        //按下旋转
        if (this.touchID1 == null && this.touchID2 == null) {
            //自行旋转
            this.currAngle -= deltaTime * 10
        } else {
            //旋转对象
            let deltaX = (this.currPos1.x - this.preX1) * 0.5
            this.preX1 = this.currPos1.x
            this.currAngle += deltaX

            //旋转摄影机
            let deltaY = (this.currPos1.y - this.preY1) * 0.005
            this.preY1 = this.currPos1.y
            this.radian += deltaY
            if (this.radian < 0.1) {
                this.radian = 0.1
            }
            if (this.radian > 3) {
                this.radian = 3
            }
        }
    }

    onTouchDown(e: EventTouch) {
        if (this.touchID1 == null) {
            this.touchID1 = e.getID()
            this.downPos1 = e.getLocation()
            this.currPos1 = this.downPos1
            this.preX1 = this.downPos1.x
            this.preY1 = this.downPos1.y
        } else if (this.touchID2 == null) {
            this.touchID2 = e.getID()
            this.downPos2 = e.getLocation()
            this.currPos2 = this.downPos2
            this.preX2 = this.downPos2.x
            this.preY2 = this.downPos2.y
        }
    }

    onTouchMove(e: EventTouch) {
        let touchs = e.getTouches()
        for (let i = 0; i < touchs.length; i++) {
            let touch = touchs[i]
            if (touch.getID() == this.touchID1) {
                this.currPos1 = touch.getLocation()
            } else if (touch.getID() == this.touchID2) {
                this.currPos2 = touch.getLocation()
            }
        }
    }

    onTouchUp(e: EventTouch) {
        if (e.getID() == this.touchID1) {
            this.touchID1 = null
        } else if (e.getID() == this.touchID2) {
            this.touchID2 = null
        }
    }

    onMouseWheel(e: EventMouse) {
        if (this.canWheel) {
            this.radius -= e.getScrollY() * 0.0005
        }
    }
}